package club.xyes.zkh.ecommerce.service.impl;

import club.xyes.zkh.ecommerce.commons.entity.Coupon;
import club.xyes.zkh.ecommerce.commons.entity.Shop;
import club.xyes.zkh.ecommerce.commons.properties.WebsiteConfigurationProperties;
import club.xyes.zkh.ecommerce.commons.util.MultipartFileUtil;
import club.xyes.zkh.ecommerce.dao.mapper.CouponMapper;
import club.xyes.zkh.ecommerce.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * Create by 郭文梁 2019/4/18 0018 13:41
 * CouponServiceImpl
 * 优惠券相关业务行为实现
 *
 * @author 郭文梁
 * @data 2019/4/18 0018
 */
@Service
public class CouponServiceImpl extends BaseServiceImpl<Coupon> implements CouponService {
    private final CouponMapper couponMapper;
    private final WebsiteConfigurationProperties websiteConfigurationProperties;

    @Autowired
    public CouponServiceImpl(CouponMapper mapper,
                             WebsiteConfigurationProperties websiteConfigurationProperties) {
        super(mapper);
        this.couponMapper = mapper;
        this.websiteConfigurationProperties = websiteConfigurationProperties;
    }

    @Override
    public List<Coupon> findByShop(Shop shop) {
        Coupon query = new Coupon();
        query.setShopId(shop.getId());
        return findByQuery(query);
    }

    @Override
    public Coupon setImage(Coupon coupon, MultipartFile imageFile) throws IOException {
        String path = websiteConfigurationProperties.getStaticFilePath() +
                websiteConfigurationProperties.getCouponImagePath();
        String filename = MultipartFileUtil.save(imageFile, path);
        coupon.setImage(filename);
        return updateById(coupon);
    }

    @Override
    public List<Coupon> findPermissionGrantsOfShop(Shop shop, Integer amount) {
        return couponMapper.selectPermissionGrantsOfShop(shop.getId(), amount);
    }

    @Override
    public List<Coupon> findAvailableByShop(Shop shop) {
        return couponMapper.findAvailableByShopId(shop.getId());
    }

    @Override
    public List<Coupon> findExpiredByShop(Shop shop) {
        return couponMapper.findExpiredByShopId(shop.getId());
    }
}
